// mircoApp/platform_order_import/platform_order_import_index/platform_order_import_index.js
var app = getApp();
import Dialog from '../../../dist/dialog/dialog';
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
Page({
  behaviors: [skinBehavior],

  /**
   * 页面的初始数据
   */
  data: {
    timer:null,
    show: false,
    sn: "",
    plugin_name: "",
    input_prompt: "",
    input_prompt_explain: "",
    introduce: "",
    success_icon: "",
    reward: [],
    timer_num:0,
    is_loading:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.data.timer_num = 0;
    if(this.data.timer){
      clearInterval(this.data.timer);
      this.data.timer = null;
    }
    this.getData();
  },


  getData() {
    app._getNetWork({
      url: app.getNetAddresss("plugin.platform-order-import.frontend.index.home"),
      success:resdata=>{
        let {result,data,msg} = resdata.data;
        if(result!==1){
          return app.tips(msg);
        }
        wx.setNavigationBarTitle({
          title: data.plugin_name
        })
        this.setData({
          plugin_name : data.plugin_name,
          input_prompt : data.input_prompt || '请输入订单号',
          input_prompt_explain : data.input_prompt_explain,
          plugin_switch : data.plugin_switch,
          introduce : data.introduce,
          success_icon : data.success_icon,
        })
      }
    });
  },
  async get_reward(oid) {
    this.setData({is_loading:true});
    if(this.data.timer_num<=30){
      let urlStr = app.getNetAddresss("plugin.platform-order-import.frontend.index.order-reward");
      let resdata = await app._getNetWork({url: urlStr,data:{oid}});
       let {result,data,msg} = resdata.data;
       if(result!==1){
         return app.tips(msg);
       }
       if (data.status !== 1) {
         if(!this.data.timer){
           this.data.timer = setInterval(() => {
            this.data.timer_num++;
             this.get_reward(oid);
           }, 1000);
         }
       } else {
         clearInterval(this.data.timer);
         this.data.timer = null;
         this.data.timer_num = 0;
         this.setData({
           reward:data.reward,
           show:true,
           is_loading:false,
         })
       }
    }else{
      clearInterval(this.data.timer);
      this.data.timer_num = 0;
      this.data.timer = null;
      Dialog.alert({
        message:'队列超时请联系商家处理',
      }).then(() => {
        // on close
      });
      this.setData({is_loading:false})
    }

  },
  goto(e) {
    let code = e.currentTarget.dataset.code;
    if (code == 'point') {
      wx.navigateTo({
        url: '/packageB/member/integral/integral',
      })
    } else if (code == 'love') {
      wx.navigateTo({
        url: '/packageB/member/love/Love_index/Love_index',
      })
    } else if (code == 'balance') {
      wx.navigateTo({
        url: '/packageA/member/balance/balance/balance',
      })
    } else if (code == 'integral') {
      wx.navigateTo({
        url: '/packageB/member/Integral_love/Integral_love',
      })
    }else if(code =='record'){
      wx.navigateTo({
        url: '/mircoApp/platform_order_import/platform_order_import_recode/platform_order_import_recode',
      })
    }
  },
  input(e){
    let sn = e.detail.value;
    this.setData({sn});
  },
  convert() {
    app._getNetWork({
      url: app.getNetAddresss("plugin.platform-order-import.frontend.index.one-touch-convert"),
      data:{sn:this.data.sn},
      success:resdata=>{
        let {result,data,msg} = resdata.data;
        if(result!==1){
          Dialog.alert({
            message: msg,
          }).then(() => {
            // on close
          });
        }else{
          this.get_reward(data.order_id)
        }
      }
    });

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    if(this.data.timer){
      clearInterval(this.data.timer);
      this.data.timer = null;
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    if(this.data.timer){
      clearInterval(this.data.timer);
      this.data.timer = null;
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  closeShow() {
    this.setData({
      show: false
    })
  }
})